<?php

$user = new User();
if ($_POST) {
	
	if (!isset($_SESSION['login']['user']) or $_SESSION['login']['user']['user_type'] != 2) {
		unset($_POST['core']['user_type'], $_POST['core']['access_rights']);
	}
	
	$user->user['core'] = array_merge($user->user['core'], $_POST['core']);
	$user->user['custom'] = array_merge($user->user['custom'], isset($_POST['custom']) ? $_POST['custom'] : array());	
	$user->user['password1'] = $_POST['password1'];
	$user->user['password2'] = $_POST['password2'];
	
	if ($user_id = $user->save($errors)) {
		// all ok
		$smarty->assign('success', true);
		$smarty->assign('user_id', $user_id);
		//header('Location: /admin/global/users/edit/' . $user_id);
	} else {
		$smarty->assign('errors', $errors);
	}
}
$smarty->assign('user', $user->user);
$smarty->assign('users_custom_schema', $user->users_custom_schema);

if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
	header('Content-type: text/xml');
	//header('Location: ');
	$smarty->display('global/users/_edit_messages.tpl.taconite');
} else {
	$smarty->display('global/users/edit.tpl.html');
}